package fr.univnantes.lina.ftbcleaner;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import fr.univnantes.lina.ftbcleaner.FTBPropertiesSingleWordCompiler;
import fr.univnantes.lina.ftbcleaner.WordAttributes;
import fr.univnantes.lina.ftbcleaner.WordCatFormOtherCounters;
import fr.univnantes.lina.ftbcleaner.WordForms;
import fr.univnantes.lina.javautil.CollectionUtilities;


public class FRBPropertiesWordFormCounter extends
		FTBPropertiesSingleWordCompiler {

	public FRBPropertiesWordFormCounter() {
		super();
	}

	/**
	 * 
	 */
	private void incrementOccurrenceCounters(WordForms aWordForms, WordAttributes aWordAttributes) {
	
		// Count the cat occurrences for a given word
		CollectionUtilities.incrementKeyCounter(
				aWordForms.getWordFormCatCounter(aWordAttributes.getWord()).getCatMap(), 
				aWordAttributes.getCat());
	
		// Count the subcat occurrences depending on the word forms + cat
		String key = aWordAttributes.getWord()+" "+ aWordAttributes.getCat();
		WordCatFormOtherCounters awcfoc = aWordForms.getWordCatFormOtherCounters(key);
		Map<String,Integer> awcfocSubcatMap= awcfoc.getSubcatMap();
		CollectionUtilities.incrementKeyCounter(
				awcfocSubcatMap, 
				aWordAttributes.getSubcat());
	
		// Count the lemma occurrences depending on the word forms + cat
		CollectionUtilities.incrementKeyCounter(
				aWordForms.getWordCatFormOtherCounters(aWordAttributes.getWord()+" "+ aWordAttributes.getCat()).getLemmaMap(), 
				aWordAttributes.getLemma());
	
		// Count the mph occurrences depending on the word forms + cat
		CollectionUtilities.incrementKeyCounter(
				aWordForms.getWordCatFormOtherCounters(aWordAttributes.getWord()+" "+ aWordAttributes.getCat()).getMphMap(), 
				aWordAttributes.getMph());
	}

	/**
	 * 
	 */
	public void sortWordFormsAttributeValues(WordForms aWordForms) {
		Iterator<String> word2CatMapIterator = aWordForms.getWords().iterator();
	
		while (word2CatMapIterator.hasNext()) {
			String wordString = word2CatMapIterator.next();
			Map<String,Integer> catOccMap = aWordForms.getWordFormCatCounter(wordString).getCatMap();
			//word2CatMap.get(wordString);
	
			if (catOccMap != null) {
				TreeMap<String,Integer> aSortedMWECatintsMap = CollectionUtilities.sortStringIntegerHashMapByValue (catOccMap);
				String firstCat = aSortedMWECatintsMap.firstKey();
	
				aWordForms.getWordFormCatCounter(wordString).setCatMap(aSortedMWECatintsMap);
	
				// which subcat is the most sailant for the couple word cat ?
				//		String key = wordString+" "+firstCat;
				//System.err.println("Debug: wordString+' '+aSortedMWECatintsMap.get(aSortedMWECatintsMap.firstKey()) = "+ key);
				TreeMap<String,Integer> aSortedMWESubcatintsMap = CollectionUtilities.sortStringIntegerHashMapByValue (aWordForms.getWordCatFormOtherCounters(wordString+" "+firstCat).getSubcatMap());
				aWordForms.getWordCatFormOtherCounters(wordString+" "+firstCat).setSubcatMap(aSortedMWESubcatintsMap);
	
				TreeMap<String,Integer> aSortedMWEMemmasMap = CollectionUtilities.sortStringIntegerHashMapByValue (aWordForms.getWordCatFormOtherCounters(wordString+" "+firstCat).getLemmaMap());
				aWordForms.getWordCatFormOtherCounters(wordString+" "+firstCat).setLemmaMap(aSortedMWEMemmasMap);
	
				TreeMap<String,Integer> aSortedMWEMphsMap = CollectionUtilities.sortStringIntegerHashMapByValue (aWordForms.getWordCatFormOtherCounters(wordString+" "+firstCat).getMphMap());
				aWordForms.getWordCatFormOtherCounters(wordString+" "+firstCat).setMphMap(aSortedMWEMphsMap);
	
			}
			else {
				System.err.println("Warning: the current word part of a multi word does not occur as a single word");
			}
		}
	}

	/**
	 * Compute stats from sw occurrences
	 * @param aWordForms TODO
	 *   
	 */
	public void countWordAttributeValues(NodeList nodeList, WordForms aWordForms) {
	
		WordAttributes currentSingleWordOccAttributes ;
	
		// for each mw occurrences
		for(int i=0; (i < nodeList.getLength()); i++) {
			if (nodeList.item(i) instanceof Element) {
				Element currentElement = (Element) nodeList.item(i);
				currentSingleWordOccAttributes = WordAttributes.element2wordAttributes(currentElement);
	
				incrementOccurrenceCounters( aWordForms,  currentSingleWordOccAttributes);
	
				//currentSingleWordOccAttributes = null;
			}
		}
	
		System.err.println("Info: # of single words occurrences whose attribute values distribution were counted = "+nodeList.getLength());
	
	}

}